import * as THREE from "../../../three/build/three.module.js"
import {OrbitControls} from "../../../three/examples/jsm/controls/OrbitControls.js"
import { ColladaLoader } from "../../../three/examples/jsm/loaders/ColladaLoader.js";
import { FBXLoader } from "../../../three/examples/jsm/loaders/FBXLoader.js";
import { GLTFLoader } from "../../../three/examples/jsm/loaders/GLTFLoader.js";
import { GUI } from "../../../three/examples/jsm/libs/dat.gui.module.js";

let renderer, camera, scene, nextFunctions=[];
let time = 0;
let config = {
    time:0,
    auto:true
};


let randomNum = [0.4684651710817136,0.9052268931552254,0.6657110723861317,0.9185378136855098,
    0.7295314052825028,0.8161218767248739,0.07573276446192612,0.12187968042693975,
    0.4882251548932395,0.03021483330629171,0.7938046085009838,0.8963420586590412,
    0.011352917485925484,0.19070003522107304,0.1284869304420866,0.25812814284518204,
    0.8703101557853148,0.019680094820653915,0.3795987045894609,0.4391937350366275,
    0.6970603033858156, 0.6354842636263991,0.27251962810671704,0.963547357255677,
    0.8465586210670311,0.6660098454095955,0.5178629114909616,0.8039182290098412,
    0.0118519624097837,0.042321558538589654,0.07762933988612875,0.07238674701862124,
    0.49739330916303315,0.8938215416920985,0.3464698508435444,0.16650409566341007,
    0.6034787783565922,0.8751925606362652,0.6333652550039215,0.6150144186559854,
    0.9994581642273346,0.05614273853012386,0.28498971841466725,0.7782388245404575,
    0.3758023902690635,0.8392532401906185,0.9991680724287513,0.41146698606783794,
    0.8419612949184181,0.6444380405129906,0.1765578232043452,0.3062804674903099,
    0.891765657376606,0.4804470319584342,0.9447693151459702,0.3501368549189112
]

let models = {iceBlocks:null, sea:null};
window.models = models;

init();
createUI();
createScene();
animate();

function init(){
    scene = new THREE.Scene();
    camera = new THREE.PerspectiveCamera(20, window.innerWidth/ window.innerHeight, 0.01, 1000);
    renderer = new THREE.WebGLRenderer({ antialias: true, logarithmicDepthBuffer: true });

    renderer.setSize(window.innerWidth, window.innerHeight);
    renderer.setClearColor(0x227777);
    document.body.appendChild(renderer.domElement);
    camera.position.set(  131.4404007427895, 9.673641929005504, -3.8698566821984137);

    new OrbitControls(camera,renderer.domElement);

    window.scene = scene;
    window.camera = camera;
    window.renderer = renderer;
}
function animate(){
    requestAnimationFrame(animate);
    next();
    render();
}

function render(){
    renderer.render(scene,camera);
}



function createScene(){

    // let colladaLoader = new FBXLoader();

    let gLTFLoader = new GLTFLoader()
    gLTFLoader.load('./model/ice_mountain2.gltf',function(dae){
        
        let model = dae.scene;
        scene.add(model);
        window.m = model;

        models.iceBlocks = model.children[7];
        models.sea = model.children[11];

        models.sea.position.x += 200;

        nextFunctions.push(iceBlocksMove);
    });


    let light = new THREE.AmbientLight(0xffffff,.5);
    scene.add(light);

    let light2 = new THREE.DirectionalLight(0xffffff,.3);
    let lightTarget = new THREE.Object3D();
    lightTarget.position.set(1,-1,-1);
    light2.target=lightTarget;
    scene.add(lightTarget);
    scene.add(light2);

}

function next(){
    if(config.auto){
        config.time = limit(++ config.time,0,3000)
    }
    time = config.time;
    for(let i=0; i< nextFunctions.length; i++){
        nextFunctions[i]();
    }
}

function iceBlocksMove(){
    let scale = 100;
    let startTime = 100;
    let sumTimeRotate = 1000;
    let sumTimePos = 2000;
    let speed = 1;

    // if(time < startTime) return;
    let iceBlocks = models.iceBlocks;

    let porcessRotate = (time - startTime)/sumTimeRotate * speed;
    porcessRotate = limit(porcessRotate,0,1);
    let rotationMax = Math.PI * 2;
    let porcessPos = (time - startTime)/sumTimePos * speed;
    porcessPos = limit(porcessPos,0,1);
    let posMax = 10;

    if(porcessRotate <= 0){
        for(let i = 0; i < iceBlocks.children.length; i++){
            let iceBlock = iceBlocks.children[i];
            iceBlock.rotation.set(0,0,0);
        }
    }else{
        for(let i = 0; i < iceBlocks.children.length; i++){
            let iceBlock = iceBlocks.children[i]
            iceBlock.rotation.set(  (randomNum[i]-.5)*porcessRotate * rotationMax,
                                    (randomNum[i+1]-.5)*porcessRotate * rotationMax,
                                    (randomNum[i+2]-.5)*porcessRotate * rotationMax);
            iceBlock.position.z = (randomNum[randomNum.length-i-1] + 1) * porcessPos * scale * posMax;
        }
    }

}

function cameraMove(){
    let cameraPositions=[
        {   
            pos:new THREE.Vector3(131.4404007427895, 9.673641929005505, -3.8698566821984297),
            rot:new THREE.Vector3(-1.9513383415297736, 1.4916940151306461, 1.9524195973931082)
        },{
            pos:new THREE.Vector3(27.096256857772154,  1.392926864251846, 10.741227632422955),
            rot:new THREE.Vector3(-0.8967877420213396, 1.4933789793634191, 0.8953244644921112)
        },{
            pos:new THREE.Vector3(26.515902941416226, 12.526418376404614, -8.764207685085553),
            rot:new THREE.Vector3(-1.841207230065056, -0.41127707249472323, -2.1770671031878837)
        },{
            pos:new THREE.Vector3(24.682185384779597, 14.104109549283162, 21.497764496493254),
            rot:new THREE.Vector3(-1.841207230065056, -0.41127707249472323, -2.1770671031878837)
        },{
            pos:new THREE.Vector3(-9.365038387839322, 14.104109549283162, -7.005216140945471),
            rot:new THREE.Vector3(-1.841207230065056, -0.41127707249472323, -2.1770671031878837)
        },{
            pos:new THREE.Vector3(-6.973808892693906, 8.30003243665689, 3.2427190275947484),
            rot:new THREE.Vector3(-1.841207230065056, -0.41127707249472323, -2.1770671031878837)
        },{
            pos:new THREE.Vector3(-11.067562072623478, 8.30003243665689, 1.8604588577473922),
            rot:new THREE.Vector3(-1.841207230065056, -0.41127707249472323, -2.1770671031878837)
        }
    ];


}

function createUI(){
    let gui = new GUI();
    gui.add(config,`auto`);
    gui.add(config,`time`,0,3000).listen();
}

function limit(num,min,max){
    num = num > min ? num : min;
    num = num < max ? num : max;
    return num;
}